//
//  HomeTableViewController.m
//  OC-LBWeibo
//
//  Created by 李博 on 15/6/24.
//  Copyright © 2015年 libo. All rights reserved.
//

#import "HomeTableViewController.h"
#import "VisitorLoginView.h"
#import "UserAccount.h"
#import "HomeTitleButton.h"
#import "PopoverAnimator.h"
#import "PopoverPresentationController.h"
#import "Status.h"
#import "SVProgressHUD.h"
#import "StatusCell.h"


///转发微博 Cell
#define LBStatusForwardCellIdentifier @"LBStatusForwardCellIdentifier"
///原创微博 Cell
#define LBStatusNormalCellIdentifier @"LBStatusNormalCellIdentifier"


@interface HomeTableViewController ()

@property (nonatomic, strong) PopoverAnimator *popoverAnimator;

@property(nonatomic,strong)NSArray *statuses;

//上拉刷新标记
@property(nonatomic,assign)BOOL pullRefreshFlag;

@end

@implementation HomeTableViewController

- (void)setStatuses:(NSArray *)statuses
{
    _statuses = statuses;
    
    //刷新表格
    [self.tableView reloadData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.visitorView setupViewInfoWithIconName:@"visitordiscover_feed_image_house" message:@"关注一些人，回这里看看有什么惊喜" isHome:YES];
    
    [self setupNavigationBar];
    [self prepareTableView];
    [self loadData];
}

///准备 tableView
- (void)prepareTableView
{
    // 注册可重用 cell
    [self.tableView registerClass:[StatusCell self] forCellReuseIdentifier:LBStatusNormalCellIdentifier];
    //取消分割线
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    // 设置行高
    self.tableView.rowHeight = 200;
    
}




///  加载数据
- (void)loadData
{
    [Status loadStatusInfoWithSinceId:0 maxId:0 finished:^(NSArray *statuses, NSError *error) {
        
        if (error != nil) {
            [SVProgressHUD showInfoWithStatus:@"网络不给力"];
            return;
        }
        if (statuses == nil) {
            NSLog(@"没有数据。。");
            return;
        }
        
        //初始化刷新
        self.statuses = statuses;
        
        NSLog(@"加载了%ld",self.statuses.count);
    }];
}

//MARK:   ------数据源------
- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.statuses.count;
}
- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath
{
    //1. 取得数据模型
    Status *status = self.statuses[indexPath.row];
    
    // ------此方法必须注册可重用cell，如果不注册，会崩溃------
    //2. 取得标示符
    StatusCell *cell = [tableView dequeueReusableCellWithIdentifier:LBStatusNormalCellIdentifier forIndexPath:indexPath];
 
    //设置代理
    
    //3. 设置Cell
    cell.status = status;
    
    //4. 判断到倒数第三行时，加载新数据
//    if (indexPath.row == self.statuses.count - 3) {
//        self.pullRefreshFlag = YES;
//        [self loadData];
//    }
    
    return cell;
}




- (void)setupNavigationBar
{
    UserAccount *userAccount = [UserAccount sharedUserAccount];
    if (userAccount == nil) {
        return;
    }
    
    //设置按钮
    UIButton *leftButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 30, 30)];
    [leftButton setImage:[UIImage imageNamed:@"navigationbar_friendsearch"] forState:UIControlStateNormal];
    [leftButton setImage:[UIImage imageNamed:@"navigationbar_friendsearch_highlighted"] forState:UIControlStateHighlighted];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:leftButton];
    
    UIButton *rightButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 30, 30)];
    [rightButton setImage:[UIImage imageNamed:@"navigationbar_pop"] forState:UIControlStateNormal];
    [rightButton setImage:[UIImage imageNamed:@"navigationbar_pop_highlighted"] forState:UIControlStateHighlighted];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:rightButton];
    
    [rightButton addTarget:self action:@selector(showQRCode) forControlEvents:UIControlEventTouchUpInside];
    
    
    //设置标题
    NSString *name = userAccount.name;
    UIButton *titleButton = [[HomeTitleButton alloc]initWithFrame:CGRectMake(0, 0, 120, 30)];
    [titleButton setTitle:name forState:UIControlStateNormal];
    [titleButton setImage:[UIImage imageNamed:@"navigationbar_arrow_down"] forState:UIControlStateNormal];
    [titleButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    
    [titleButton addTarget:self action:@selector(titleBtnClicked) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.titleView = titleButton;
    
}

///标题按钮点击事件
- (void)titleBtnClicked
{
    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"FriendGroup" bundle:nil];
    UIViewController *vc = [sb instantiateViewControllerWithIdentifier:@"FriendGroupSB"];
    self.popoverAnimator = [[PopoverAnimator alloc]init];
    vc.transitioningDelegate = self.popoverAnimator;
    
    //设置视图的展现大小
    CGFloat x = (self.view.bounds.size.width - 200)* 0.5;
    self.popoverAnimator.presentFrame = CGRectMake(x, 56, 200, 300);
    //3.设置转场动画
    vc.modalPresentationStyle = UIModalPresentationCustom;
    
    [self presentViewController:vc animated:YES completion:nil];

}

- (void)showQRCode
{
    NSLog(@"二维码。。");
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
    [self.visitorView startAnimation];
}



@end
